package com.wpre.lotterysystem.service.activitystatus.operator;

import com.wpre.lotterysystem.controller.result.GetActivityDetailResult;
import com.wpre.lotterysystem.dao.dataobject.ActivityUserDO;
import com.wpre.lotterysystem.dao.mapper.ActivityUserMapper;
import com.wpre.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.wpre.lotterysystem.service.enums.ActivityUserStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-12-24
 * Time: 20:03
 */
@Component
public class UserOperator extends AbstractActivityOperator {

    @Autowired
    private ActivityUserMapper activityUserMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        List<Long> ids = convertActivityStatusDTO.getIds();
        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityUserStatusEnum userStatus = convertActivityStatusDTO.getUserStatus();
        if (ids == null) {
            return false;
        }
        if (activityId == null) {
            return false;
        }
        if (userStatus == null) {
            return false;
        }
        List<ActivityUserDO> userDOS = activityUserMapper.batchSelectUserByUserIdAndAId(activityId, ids);
        if (userDOS == null) {
            return false;
        }
        for (ActivityUserDO auDO : userDOS) {
            if (auDO.getStatus().equalsIgnoreCase(convertActivityStatusDTO.getUserStatus().name())) {
                return false;
            }
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        List<Long> ids = convertActivityStatusDTO.getIds();
        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityUserStatusEnum userStatus = convertActivityStatusDTO.getUserStatus();
        try {
            activityUserMapper.batchUpdateStatus(activityId, ids, userStatus.name());
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
